package handle

func GetToken(grantType, clientID, clientSecret string) (*Token, error) {
	url := "http://dev.meirenji.cn/oauth/token"
	payload := "grant_type=" + grantType + "&client_id=" + clientID + "&client_secret=" + clientSecret
	var token Token
	err := MakeRequest(url, payload, &token)
	if err != nil {
		panic(err)
	}
	return &token, nil
}

func GetOpenID(appID, accessToken string) ([]ContentItem, error) {
	url := "http://dev.meirenji.cn/api/v2/instances"
	payload := "appId=" + appID + "&access_token=" + accessToken
	var instances Instances
	err := MakeRequest(url, payload, &instances)
	if err != nil {
		panic(err)
	}
	return instances.Content, nil
}

func GetOpenDetail(appID, accessToken, openId string) (*Detail, error) {
	url := "http://dev.meirenji.cn/api/v2/instance/detail"
	payload := "access_token=" + accessToken + "&appId=" + appID + "&openId=" + openId
	var detail Detail
	err := MakeRequest(url, payload, &detail)
	if err != nil {
		panic(err)
	}
	return &detail, nil
}

func GetInOut(appID, accessToken, openId, passagewayId, tim string) (*InOutData, error) {
	url := "http://dev.meirenji.cn/api/v2/passageway/traffic/five"
	payload := "access_token=" + accessToken + "&appId=" + appID + "&openId=" + openId + "&passagewayId=" + passagewayId + "&start=" + tim + "&end=" + tim
	var outdated InOutData
	err := MakeRequest(url, payload, &outdated)
	if err != nil {
		panic(err)
	}
	return &outdated, nil
}
